package cn.onein.edu.web.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.onein.edu.biz.bean.ContentTags;
import cn.onein.edu.biz.bean.LearnContent;
import cn.onein.edu.web.dao.LearnContentDao;
import cn.onein.edu.web.service.LearnContentService;
import cn.onein.edu.web.util.SearchParamUtil;

/**
 * 育儿百科查询、Tag和文章 service接口
 * @author Upton
 *
 */
@Service
public class LearnContentServiceImpl implements LearnContentService {

	Logger logger = Logger.getLogger(LearnContentServiceImpl.class);
	
	private static final String HOT_TAG_LINK_TEMPLATE = "<a class='a-hot-tags' href='../tag/%s' target='_blank'>%s</a>";
	
	private static final String HOT_TAG_LINK_TEMPLATE_WITHSPACE = "<a class='a-hot-tags' href='../tag/%s' target='_blank'>%s</a>&nbsp;&nbsp;";

	@Autowired
	private LearnContentDao learnContentDao;
	
	@Override
	public LearnContent learnContentById(Long id) {
		LearnContent content = learnContentDao.getLearnContentById(id);
		if(content != null){
			learnContentDao.updateContentHit(id);
			
			//为hot tag设置link
			List<ContentTags> hotTags = learnContentDao.getHotTags();
			StringBuffer fullText = new StringBuffer(content.getFullText());
			for(ContentTags tag : hotTags){
				String tagName = tag.getTagName();
				int index;
				if((index = fullText.indexOf(tagName)) >= 0 ){
					String link = "";
					try {
						link = String.format(HOT_TAG_LINK_TEMPLATE, new Object[]{URLEncoder.encode(tagName,"UTF-8"),tagName});
					} catch (UnsupportedEncodingException e) {
						logger.error("tagword encode error", e);
						link = "";
					}
					if(!"".equals(link)){
						fullText.replace(index, index + tagName.length(), link);
					}
				}
			}
			content.setFullText(fullText.toString());
			
			//设置tag list link
			List<String> tagList = content.getTagList();
			StringBuffer buffer = new StringBuffer();
			for(String tagName : tagList){
				String link = "";
				try {
				   link = String.format(HOT_TAG_LINK_TEMPLATE_WITHSPACE, new Object[]{URLEncoder.encode(tagName,"UTF-8"),tagName});
				} catch (UnsupportedEncodingException e) {
					logger.error("tagword encode error", e);
					link = "";
				}
				if(!"".equals(link)){
					buffer.append(link);
				}				
			}
			content.setTags(buffer.toString());
		}
		
		return content;
	}
	
	@Override
	public Integer getContentCountByTag(String tagword){
		return learnContentDao.getContentCountBytag(tagword);
	}

	@Override
	public List<LearnContent> getContentListBytag(String tagword,Integer currPage,Integer perpageSize) {
		Integer beginIndex = (currPage-1) * perpageSize;
		return learnContentDao.getContentListBytag(tagword,perpageSize,beginIndex);
	}
	
	@Override
	public void getSearchContent(Map<String,Object> map,String search,Integer currPage,Integer perpageSize,String path){
		Integer beginIndex = (currPage-1) * perpageSize;
		
		SearchParamUtil param = new SearchParamUtil();
		param.setParam(search);
		param.setPerPage(perpageSize);
		param.setBeninIndex(beginIndex);
		
		List<LearnContent> contents = learnContentDao.getContentListBySearch(param);
		
		for(LearnContent o : contents){
			List<String> tagList = o.getTagList();
			StringBuffer buffer = new StringBuffer();
			for(String tag : tagList){
				String urlEncode = "";
				try {
				   urlEncode = URLEncoder.encode(tag,"UTF-8");
				} catch (UnsupportedEncodingException e) {
					logger.error("tagword encode error", e);
					urlEncode = "";
				}
				if(!"".equals(urlEncode)){
					buffer.append("<a class='a-hot-tags' href='"+path+"/"+urlEncode+"'>"+tag+"</a>&nbsp;&nbsp;");
				}				
			}
			o.setTags(buffer.toString());
		}
		map.put("contents", contents);
	}
	
	@Override
	public Integer getContentCountBySearch(String search){
		return learnContentDao.getContentCountBySearch(search);
	}
	
	@Override
	public List<ContentTags> getHotTags(){
		List<ContentTags> hotTags = learnContentDao.getHotTags();
		
		return hotTags;
	}

	@Override
	public List<LearnContent> getHotContentByHits(int count) {
		return learnContentDao.getHotContentByHits(count);
	}
}